package eerbaar.extension;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ExtensionPackage implements IExtensionPackage {

    private final Map<Class<?>, IExtensible> map = new HashMap<Class<?>, IExtensible>();

    public ExtensionPackage() {
        //
    }

    @Override
    public IExtensible copy() {

        ExtensionPackage result = new ExtensionPackage();

        Iterator<Class<?>> i = this.map.keySet().iterator();
        while (i.hasNext()) {

            Class<?> type = i.next();
            IExtensible e = this.map.get(type);

            IExtensible c = e.copy();
            result.installExtension(type, c);
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T extends IExtensible> T getExtension(Class<T> type) {

        return (T) this.map.get(type);
    }

    @Override
    public void installExtension(Class<?> type, IExtensible extension) {

        this.map.put(type, extension);
    }

    @Override
    public void uninstallExtension(Class<?> type) {

        this.map.remove(type);
    }

}
